﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using V5_BLL;
using V5_Model;
using V5_Common;

namespace V5_EShop.Admin
{
    public partial class Admin_AskAnswer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.Bind_gvList();
            }
        }

        private void Bind_gvList()
        {
            this.MultiViewList.ActiveViewIndex = 0;
            int Count = 0;
            string strWhere = "";
            DataTable dt = new BLLProductAsk().GetList(Pager.PageSize,
                Pager.CurrentPageIndex,
                strWhere,
                ref Count).Tables[0];
            Pager.RecordCount = Count;
            this.gvList.DataSource = dt;
            this.gvList.DataBind();
        }

        protected void Pager_PageChanged(object sender, EventArgs e)
        {
            this.Bind_gvList();
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="oUserID"></param>
        /// <returns></returns>
        public ModelUser Get_User(object oUserID)
        {
            int UserID = InputHelper.SetInt(oUserID);
            if (UserID > 0)
            {
                return new BLLUser().GetModel(UserID);
            }
            return null;
        }

        /// <summary>
        /// 获取产品信息
        /// </summary>
        /// <param name="oProductID"></param>
        /// <returns></returns>
        public ModelProduct Get_Product(object oProductID)
        {
            int ProductID = InputHelper.SetInt(oProductID);
            if (ProductID > 0)
            {
                return new BLLProduct().GetModel(ProductID);
            }
            return null;
        }


        private void Bind_DataEdit(int id)
        {
            this.MultiViewList.ActiveViewIndex = 1;
            ModelProductAsk m_ask = new BLLProductAsk().GetModel(id);
            if (m_ask != null)
            {
                lblProductName.Text = this.Get_Product(m_ask.ProductID).ProductName;
                lblAskTitle.Text = m_ask.AskTitle;
                lblUserName.Text = this.Get_User(m_ask.MemberID).UserName;
                lblGetIP.Text = m_ask.GetIP;
                this.rbtnListPassed.SelectedIndex = this.rbtnListPassed.Items.IndexOf(this.rbtnListPassed.Items.FindByValue(m_ask.Passed.ToString()));
                this.hfID.Value = m_ask.ID.ToString();
            }
        }

        private void Bind_Delete(int id)
        {
            new BLLProductAsk().Delete(id);
            this.Bind_gvList();
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            int id = InputHelper.SetInt(this.hfID.Value);
            int passed = InputHelper.SetInt(this.rbtnListPassed.SelectedValue
                );
            if (id > 0)
            {
                ModelProductAsk m_ask = new BLLProductAsk().GetModel(id);
                m_ask.Passed = passed;
                new BLLProductAsk().Update(m_ask);
            }
            this.Bind_gvList();
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string eName = InputHelper.SetString(e.CommandName);
            int id = InputHelper.SetInt(e.CommandArgument);

            switch (eName.ToLower())
            {
                case "golist":
                    Response.Redirect("Admin_ProductAnswer.aspx?askid="+id);
                    break;
                case "go":
                    this.Bind_DataEdit(id);
                    break;
                case "del":
                    this.Bind_Delete(id);
                    break;
            }
        }
    }
}
